package com.netcracker.ui.command;

import com.netcracker.entity.Right;
import com.netcracker.util.service.PublisherService;
import com.netcracker.util.service.ServiceFacade;
import com.netcracker.util.service.exception.DataTransmittingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.slf4j.LoggerFactory;

public class DeletePublisherCommand implements Command {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(DeletePublisherCommand.class);
    private final ServiceFacade serviceFacade;

        public DeletePublisherCommand(ServiceFacade serviceFacade) {
        this.serviceFacade = serviceFacade;
    }
    
    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) {
        Right right = (Right) request.getSession().getAttribute("right");
        if (right != null && right.equals(Right.ADMIN)) {
            Integer publisherID = Integer.parseInt(request.getParameter("publisherID"));
            PublisherService publisherService = serviceFacade.getPublisherService();
            try {
                publisherService.deletePublisher(publisherID);
                request.setAttribute("success", "Издательство удалено.");
            } catch (DataTransmittingException ex) {
                request.setAttribute("error", "Ошибка обращения к БД.");
                LOGGER.error("DB error: ", ex);
            }
        }   
        return "AdminPublishers.jsp";
    }
}
